package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;

import com.fondesa.recyclerviewdivider.Line;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.TenderAllListEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ResolutionUtil;

import java.math.BigDecimal;
import java.util.List;

public class PickMapAllAdapter extends BaseRecycleAdapter<TenderAllListEntity> {

    private static final int TYPE_DEFAULT = 1;
    private static final int TYPE_YU = 2;

    private CallBack callBack;
    private int width;
    private String money;
    private TextView tvUserFocus;
    private ImageView imageUserFocus;

    public PickMapAllAdapter(Context context, List<TenderAllListEntity> datas, CallBack callBack) {
        super(context, 0, datas);
        width = ResolutionUtil.getScreenWidth(context) / 20 * 19;
        this.callBack = callBack;
    }

    @Override
    public int getItemViewType(int position) {
        TenderAllListEntity entity = datas.get(position);
        if (entity.getBusinessType() == 3) {
            return TYPE_YU;
        }
        return TYPE_DEFAULT;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_DEFAULT) {
            return ItemViewHolder.get(context, parent, R.layout.item_map_pick);
        } else {
            return ItemViewHolder.get(context, parent, R.layout.item_map_pick_yu);
        }
    }

    @Override
    public void convert(ItemViewHolder holder, TenderAllListEntity tenderAllListEntity) {
        holder.setBinding(BR.viewModel, tenderAllListEntity);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        int itemViewType = getItemViewType(position);
        TenderAllListEntity entity = datas.get(position);

        if (holder.itemView.getLayoutParams().width != width) {
            holder.itemView.getLayoutParams().width = width;
        }

        TextView name = holder.itemView.findViewById(R.id.name);
        TextView tvType = holder.itemView.findViewById(R.id.tv_type);
        TextView tvTypeVertical = holder.itemView.findViewById(R.id.tv_type_vertical);
        TextView tvState = holder.itemView.findViewById(R.id.tv_state);
        TextView tvSpace = holder.itemView.findViewById(R.id.tv_space);
        TextView tvTimeTitle = holder.itemView.findViewById(R.id.tv_time_title);
        TextView tvTimeContext = holder.itemView.findViewById(R.id.tv_time_context);
        TextView tvMoneyTitle = holder.itemView.findViewById(R.id.tv_money_title);
        TextView tvMoneyContext = holder.itemView.findViewById(R.id.tv_money_context);
        TextView tvCompanyTitle = holder.itemView.findViewById(R.id.tv_company_title);
        TextView tvCompanyContext = holder.itemView.findViewById(R.id.tv_company_context);

        name.setText(entity.getName());
        tvState.setText(entity.getStateName());

        if (!TextUtils.isEmpty(entity.getMoney())) {
            BigDecimal num = new BigDecimal(entity.getMoney());
            money = num.stripTrailingZeros().toPlainString();
        } else {
            money = "0";
        }

        switch (itemViewType) {
            case TYPE_DEFAULT:
                TextView tvCommit = holder.itemView.findViewById(R.id.tv_commit);
                if (entity.getBusinessType() == 1) {   // 1.招标 2.竞价 3.需求预告

                    tvType.setText("招标");
                    tvTypeVertical.setText(entity.getTypeName());
                    tvSpace.setText("距您" + entity.getDistance());
                    tvMoneyTitle.setText("概算金额：");
                    tvMoneyContext.setText(money + "元");
                    tvCompanyTitle.setText("招标企业：");
                    tvCompanyContext.setText(entity.getComName());
//                    tvAddressContext.setText(entity.getRegion() + entity.getAddress());

                    switch (entity.getState()) {
                        case 2:
                        case 6:
                            tvTimeTitle.setText("投标结束时间：");
                            tvTimeContext.setText(TextUtils.isEmpty(entity.getEndTimeStr(2)) ? "" : entity.getEndTimeStr(2));
                            updateViewData(tvCommit, "我要投标", 0);
                            break;
                        case 4:
                            tvTimeTitle.setText("定标时间：");
                            tvTimeContext.setText(TextUtils.isEmpty(entity.getUpdatedStr(2)) ? "" : entity.getUpdatedStr(2));
                            updateViewData(tvCommit, "我要投标", 1);
                            break;
                        case 9:
                            tvTimeTitle.setText("报名结束时间：");
                            tvTimeContext.setText(TextUtils.isEmpty(entity.getApplyEndTimeStr(2)) ? "" : entity.getApplyEndTimeStr(2));
                            updateViewData(tvCommit, "我要报名", 0);
                            break;
                        case 3:
                            tvTimeTitle.setText("开标时间：");
                            tvTimeContext.setText(TextUtils.isEmpty(entity.getBidTimeStr(2)) ? "" : entity.getBidTimeStr(2));
                            updateViewData(tvCommit, "我要投标", 1);
                            break;
                        case 1:
                            tvTimeTitle.setText("投标开始时间：");
                            tvTimeContext.setText(TextUtils.isEmpty(entity.getStartTimeStr(2)) ? "" : entity.getStartTimeStr(2));
                            updateViewData(tvCommit, "我要投标", 1);
                            break;
                        case 8:
                            tvTimeTitle.setText("报名开始时间：");
                            tvTimeContext.setText(TextUtils.isEmpty(entity.getApplyStartTimeStr(2)) ? "" : entity.getApplyStartTimeStr(2));
                            updateViewData(tvCommit, "我要报名", 1);
                            break;
                    }


                } else if (entity.getBusinessType() == 2) {

                    tvType.setText("竞价");
                    tvTypeVertical.setText(entity.getTypeName());
                    tvSpace.setText("距您" + entity.getDistance());
                    tvMoneyTitle.setText("概算金额：");
                    tvMoneyContext.setText(money + "元");
                    tvCompanyTitle.setText("采购企业：");
                    tvCompanyContext.setText(entity.getComName());
//                    tvAddressContext.setText(entity.getRegion() + entity.getAddress());


                    if (2 == entity.getState()) {
                        tvTimeTitle.setText("截止时间：");
                        tvTimeContext.setText(TextUtils.isEmpty(entity.getDeadlineTime(2)) ? "" : entity.getDeadlineTime(2));
                        updateViewData(tvCommit, "我要竞价", 0);
                    } else if (3 == entity.getState()) {
                        tvTimeTitle.setText("定标时间：");
                        tvTimeContext.setText(TextUtils.isEmpty(entity.getUpdatedStr(2)) ? "" : entity.getUpdatedStr(2));
                        updateViewData(tvCommit, "我要竞价", 1);
                    } else if(5 == entity.getState()){
                        tvTimeTitle.setText("截止时间：");
                        tvTimeContext.setText(TextUtils.isEmpty(entity.getDeadlineTime(2)) ? "" : entity.getDeadlineTime(2));
                        updateViewData(tvCommit, "我要竞价", 1);
                    }

                }

                imageUserFocus = holder.itemView.findViewById(R.id.image_user_focus);
                tvUserFocus = holder.itemView.findViewById(R.id.tv_user_focus);
                updateFocus(entity.getUserFocusFlag());

                holder.itemView.findViewById(R.id.tv_report).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        callBack.onReport(entity);
                    }
                });
                holder.itemView.findViewById(R.id.tv_contact_service).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        callBack.onContactService(entity);
                    }
                });
                holder.itemView.findViewById(R.id.tv_inquire_by_letter).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        callBack.onInquireByLetter(entity);
                    }
                });
                holder.itemView.findViewById(R.id.line_user_focus).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        callBack.onLineUserFocus(entity, holder.getLayoutPosition());
                    }
                });
                tvCommit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        callBack.onClick(entity);
                    }
                });
                break;
            case TYPE_YU:
                TextView tvAddressContext = holder.itemView.findViewById(R.id.tv_address_context);
                tvType.setText("需求预告");
                tvTypeVertical.setText(entity.getModuleType());
                tvSpace.setText("距您" + entity.getDistance());
                tvState.setText("询价中");
                tvMoneyTitle.setText("项目造价：");
                tvMoneyContext.setText(money + "元");
                tvCompanyTitle.setText("发布企业：");
                tvCompanyContext.setText(entity.getComName());
                tvAddressContext.setText(entity.getAddress());

                tvTimeTitle.setText("发布时间：");
                tvTimeContext.setText(TextUtils.isEmpty(entity.getCreatedStr(2)) ? "" : entity.getCreatedStr(2));
                break;
        }

        holder.itemView.findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onDismiss();
            }
        });
        LinearLayout layout = holder.itemView.findViewById(R.id.line);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onClick(entity);
            }
        });
        CardView cardView = holder.itemView.findViewById(R.id.card_view);
        cardView.post(new Runnable() {
            @Override
            public void run() {
                if (cardView.getHeight() < mLineHeight) {
                    cardView.setMinimumHeight(mLineHeight);
                    cardView.requestLayout();
                } else {
                    mLineHeight = cardView.getHeight();
                }
            }
        });
    }

    private int mLineHeight;

    public void updateFocus(int userFocusFlag) {
        if (userFocusFlag == 0) {
            imageUserFocus.setBackgroundResource(R.mipmap.guanzhu_pick);
            tvUserFocus.setText("关注");
            tvUserFocus.setTextColor(context.getResources().getColor(R.color.color_tab_one));
        } else if (userFocusFlag == 1) {
            imageUserFocus.setBackgroundResource(R.mipmap.yiguanzhu_pick);
            tvUserFocus.setText("已关注");
            tvUserFocus.setTextColor(context.getResources().getColor(R.color.color_main));
        }
    }

    public void updateFocusForPosition(int position) {
        int userFocusFlag1 = datas.get(position).getUserFocusFlag();
        datas.get(position).setUserFocusFlag(userFocusFlag1 == 0 ? 1 : 0);
        notifyItemChanged(position);
    }

    private void updateViewData(TextView tvCommit, String tvCommitText, int tvCommitBg) {
        if (!TextUtils.isEmpty(tvCommitText)) {
            tvCommit.setText(tvCommitText);
        } else {
            tvCommit.setText("");
        }
        if (tvCommitBg == 0) {
            tvCommit.setBackground(ContextCompat.getDrawable(context, R.drawable.tender_details));
            tvCommit.setEnabled(true);
        } else {
            //if(tvCommitBg == 1)
            tvCommit.setBackground(ContextCompat.getDrawable(context, R.drawable.tender_dateils_unclick));
            tvCommit.setEnabled(false);
        }
    }

    public interface CallBack {
        void onDismiss();

        void onClick(TenderAllListEntity tenderAllListEntity);

        void onReport(TenderAllListEntity tenderAllListEntity);

        void onContactService(TenderAllListEntity tenderAllListEntity);

        void onInquireByLetter(TenderAllListEntity tenderAllListEntity);

        void onLineUserFocus(TenderAllListEntity tenderAllListEntity, int position);
    }
}
